<?php
/*
 * template/edit.phtml
 */

use RHX\Helper\RHDateTime;
use RHX\Model\Object\EmployeTemplate;

$templates		 = $this->templates;
$planningTemplate	 = $this->planningTemplate;
$codec			 = $this->codec;
$codes			 = $this->codes;
$nohead			 = $this->nohead;
$fullscreen		 = $this->fullscreen;
// Correctif
$codec[0] = $codec[3];
//var_dump($nohead);
//var_dump($fullscreen);
$templateList		 = array();
$dayName		 = array(
    'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche',
);
?>
<div class="container-fluid">
    <?php if (!$nohead): ?>
        <div class="content-head">
            <form id="edit-template" action="#" method="post">
    	    <h3>
    		Planning Type : <input type='text' name='libelle' value='<?php echo $planningTemplate->libelle; ?>' style='width: 400px'>
    	    </h3>
    	    <input type="hidden" name="id" value="<?php echo $planningTemplate->id; ?>">
    	    <input type="hidden" name="site_id" value="<?php echo $planningTemplate->site_id; ?>">
    	    <input type="hidden" name="template" value="<?php echo $planningTemplate->template; ?>">
    	    <input type="hidden" name="creation" value="<?php echo $planningTemplate->creation; ?>">
    	    <input type="hidden" name="edition" value="<?php echo date('Y-m-d'); ?>">
	    <input type="hidden" name="actif" value="<?php echo $planningTemplate->actif; ?>">
	    <input type="hidden" name="parent" value="<?php echo $planningTemplate->parent; ?>">
    	    <div class="toolbar">
    		<button class="btn btn-success rename">Renommer</button>
    		<button class="btn btn-inverse goback">Fermer</button>
    	    </div>
            </form>
        </div>
    <?php endif; ?>
    <?php if ($fullscreen): ?>
        <div class="gobackBtn">
            <button class="btn btn-info">Retour</button>
        </div>
    <?php endif; ?>
    <div class="content-body">
	<div>
	    <table class="table table-condensed list-table black-link" style="font-size: 0.9em;">
		<thead>
		    <tr>
			<td rowspan='2'>Mat.</td>
			<td rowspan='2'>Poste</td>
			<td rowspan='2'>Prénom</td>
			<td rowspan='2'>Contrat Horaire</td>
			<td colspan='5' style="border-right: solid 2px #DDDDDD">Lundi</td>
			<td colspan='5' style="border-right: solid 2px #DDDDDD">Mardi</td>
			<td colspan='5' style="border-right: solid 2px #DDDDDD">Mercredi</td>
			<td colspan='5' style="border-right: solid 2px #DDDDDD">Jeudi</td>
			<td colspan='5' style="border-right: solid 2px #DDDDDD">Vendredi</td>
			<td colspan='5' style="border-right: solid 2px #DDDDDD">Samedi</td>
			<td colspan='5' style="border-right: solid 2px #DDDDDD">Dimanche</td>
			<td rowspan='2'>Cumul des heures</td>
		    </tr>
		    <tr>
			<?php foreach ($dayName as $day): ?>
    			<td>Code</td>
    			<td>Entrée</td>
    			<td>Sortie</td>
    			<td>P</td>
    			<td style="border-right: solid 2px #DDDDDD">R</td>
			<?php endforeach; ?>
		    </tr>
		</thead>
		<tbody>
		    <?php $jaune = 1; ?>
		    <?php foreach ($templates as $tpl): ?>
			<?php $templateList[]	 = $tpl; ?>
			<?php $e		 = $tpl->getEmploye(); ?>
			<?php $poste		 = $e->getPoste(); ?>
    		    <tr>
    			<td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>>
			    <a href="<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'edit', 'id' => $e->matricule)) ?>" target="_blank"><?php echo $e->matricule; ?></a>
			</td>
    			<td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>><?php echo $poste->code; ?></td>
    			<td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>>
			    <a href="<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'edit', 'id' => $e->matricule)) ?>" target="_blank">
				<?php echo $e->prenom ?>
			    </a>
			    </td>
    			<td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>><b><?php echo $e->getHoraireHebdo(true); ?></b></td>

			    <?php foreach ($dayName as $day): ?>
				<?php
				$dayCode	 = $day . '_code';
				$dayEntree	 = $day . '_entree';
				$daySortie	 = $day . '_sortie';
				$dayPause	 = $day . '_pause';
				$dayRepas	 = $day . '_repas';
				?>
				<td class='<?php echo $codec[$tpl->$dayCode]; ?>' id='<?php echo $e->matricule . '-' . $day . '-code'; ?>'>
				    <a href='editer planning Type' class='template-edit' data-matricule='<?php echo $e->matricule ?>' data-id='<?php echo $tpl->id; ?>' data-prefix='<?php echo $day ?>'><?php echo $codec[$tpl->$dayCode]; ?></a>
				</td>
				<td class='<?php echo $codec[$tpl->$dayCode]; ?>' id='<?php echo $e->matricule . '-' . $day . '-entree'; ?>'>
				    <a href='editer planning Type' class='template-edit' data-matricule='<?php echo $e->matricule ?>' data-id='<?php echo $tpl->id; ?>' data-prefix='<?php echo $day ?>'><?php echo RHDateTime::hourNoSecond($tpl->$dayEntree); ?></a>
				</td>
				<td class='<?php echo $codec[$tpl->$dayCode]; ?>' id='<?php echo $e->matricule . '-' . $day . '-sortie'; ?>'>
				    <a href='editer planning Type' class='template-edit' data-matricule='<?php echo $e->matricule ?>' data-id='<?php echo $tpl->id; ?>' data-prefix='<?php echo $day ?>'><?php echo RHDateTime::hourNoSecond($tpl->$daySortie); ?></a>
				</td>
				<td class='<?php echo $codec[$tpl->$dayCode]; ?>' id='<?php echo $e->matricule . '-' . $day . '-pause'; ?>'>
				    <a href='editer planning Type' class='template-edit' data-matricule='<?php echo $e->matricule ?>' data-id='<?php echo $tpl->id; ?>' data-prefix='<?php echo $day ?>'><?php echo ((int) $tpl->$dayPause == 1) ? 'X' : ''; ?></a>
				</td>
				<td class='<?php echo $codec[$tpl->$dayCode]; ?>' id='<?php echo $e->matricule . '-' . $day . '-repas'; ?>' style="border-right: solid 2px #DDDDDD">
				    <a href='editer planning Type' class='template-edit' data-matricule='<?php echo $e->matricule ?>' data-id='<?php echo $tpl->id; ?>' data-prefix='<?php echo $day ?>'><?php echo ((int) $tpl->$dayRepas == 1) ? '1' : ''; ?></a>
				</td>
			    <?php endforeach; ?>

    			<td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>>
    			    <span id='<?php echo $e->matricule ?>-cumul'></span>
    			</td>
    		    </tr>
			<?php $jaune++; ?>
		    <?php endforeach; ?>
		</tbody>
	    </table>
	    <!--div class="evolution">
		<div class="title">Sélectionner une version du planning Type</div>
		<select id="template-evolution">
		    <?php
		    $versionMax	 = $this->versionMax;
		    $version	 = $this->version;
		    for ($i = 1; $i <= $versionMax; $i++):
			?>
			<?php $selected = ($version == $i) ? ' selected="selected"' : ''; ?>
    		    <option value="<?php echo $i; ?>"<?php echo $selected; ?>>Version <?php echo $i; ?></option>
		    <?php endfor; ?>
		</select>
	    </div-->
	</div>
    </div>
</div>

<script type="text/javascript">
// Démarrage
    $(function() {
	// Goback
	$('.goback, .gobackBtn').click(function(e) {
	    e.preventDefault();
	    normalScreenLayout();
	});
	// Renommer
	$('.rename').click(function(e) {
	    e.preventDefault();
	    $(this).addClass('waiting-icon');
	    ajax = $.ajax({
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller' => 'template', 'action' => 'rename')) ?>',
		data: $('#edit-template').serialize()
	    });
	    ajax.done(function() {
		$(this).removeClass('waiting-icon');
		normalScreenLayout();
		refresh();
	    });
	    ajax.fail(function() {
		$(this).removeClass('waiting-icon');
		alert("Erreur lors de la mise à jour !");
	    });
	});
	$('.template-edit').click(function(e) {
	    e.preventDefault();
	    $('.popover').remove();
	});
<?php if (!$nohead) : ?>
    	    //return false;
	    $('.template-edit').popover({
	    html: true,
	    title: '<center>Edition du Planning Type</center>',
	    placement: 'bottom',
	    content: function() {
		id = $(this).attr('data-id');
		matricule = $(this).attr('data-matricule');
		prefix = $(this).attr('data-prefix');

		ajax = $.ajax({
		    type: 'post',
		    url: '<?php echo $this->url('RHX/default', array('controller' => 'template', 'action' => 'popover')) ?>',
		    data: '&matricule=' + matricule + '&prefix=' + prefix + '&id=' + id
		});
		ajax.done(function(result) {
		    $('.popover-content').html(result);
		});
	    }
	});
<?php endif; ?>

	// Edition
	$('.template-edit_').click(function(event) {
	    event.preventDefault();
<?php if ($nohead) : ?>
    	    return false;
<?php endif; ?>
	    // Récupération des variables
	    matricule = $(this).attr('data-matricule');
	    id = $(this).attr('data-id');
	    prefix = $(this).attr('data-prefix');
	    // Position de la souris
	    x = event.pageX;
	    y = event.pageY;
	    // Taille de l'écran
	    sw = $(window).width();
	    sh = $(window).height();
	    // Taille du dialogue
	    dw = 500;
	    dh = 400;
	    // Positionnement du dialogue
	    px = 0;
	    py = 0;
	    // Test sur la largeur
	    if ((sw - (dw + x)) > 0) {
		px = x;
	    } else {
		px = x - dw;
	    }
	    // Test sur la hauteur
	    if ((sh - y) >= dh - 400) {
		py = y /*- 40*/;
	    } else {
		py = y - dh /*+ 80*/;
	    }
	    // Suppression des autres DIV conteneur
	    $('.rhPopover').remove();
	    // Ajout d'un div conteneur
	    $('#' + matricule + '-' + prefix + '-code').append("<div class='rhPopover'></div>");
	    // Chargement du contenu
	    ajax = $.ajax({
		type: 'post',
		url: '<?php echo $this->url('RHX/default', array('controller' => 'template', 'action' => 'popin')) ?>',
		data: "&prefix=" + prefix + '&matricule=' + matricule + "&id=" + id
	    });
	    ajax.success(function(data) {
		$('.rhPopover').hide().html(data).css({
		    'top': py,
		    'left': px
		}).show('normal');
	    });
	    ajax.error(function() {
		alert("Erreur de chargement");
	    });
	});

	// Version
	$('#template-evolution').change(function() {
	    version = $(this).val();
	    template = <?php echo $this->template ?>;
	    $('#template-' + id).attr('data-version', version);
	    $('#template-' + id).click();
	});

	// Calcul des Cumules d'heures
<?php foreach ($templateList as $tpl): ?>
    	calculateCumul(<?php echo $tpl->id; ?>, <?php echo $tpl->matricule; ?>);
<?php endforeach; ?>
    });
// Les Jours de la semaine
    days = new Array('dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi');

// Fonction de Calcul des Cumuls des heures
    function cumulHeure(id, matricule) {
	ajax = $.ajax({
	    dataType: 'json',
	    type: 'post',
	    url: '<?php echo $this->url('RHX/default', array('controller' => 'template', 'action' => 'calculateCumul')) ?>',
	    data: '&id=' + id + '&matricule=' + matricule
	});
	ajax.done(function(data) {
	    $('#' + matricule + '-cumul').hide().fadeIn('normal').html(data.cumul);
	});
    }

// Nouvelle Fonction
    function calculateCumul(id, matricule) {
	ajax = $.ajax({
	    dataType: 'json',
	    type: 'post',
	    url: '<?php echo $this->url('RHX/default', array('controller' => 'calculator', 'action' => 'cumulTemplate')) ?>',
	    data: '&json=on&save=on&id=' + id
	});
	ajax.done(function(data) {
	    template = data.template;
	    state = data.state;
	    switch (state) {
		case <?php echo EmployeTemplate::EQUAL ?>:
		    $('#' + matricule + '-cumul').parent().removeClass().addClass('value-equal');
		    break;
		case <?php echo EmployeTemplate::GREATER_THAN ?>:
		    $('#' + matricule + '-cumul').parent().removeClass().addClass('value-greater');
		    break;
		case <?php echo EmployeTemplate::LESS_THAN ?>:
		    $('#' + matricule + '-cumul').parent().removeClass().addClass('value-less');
		    break;
	    }
	    $('#' + matricule + '-cumul').hide().fadeIn('normal').html(template.heure_prevues);
	});
    }
</script>

<style type="text/css">
    .jaune1{
	background: #FFF67F;
    }
    .jaune2{
	background: #FFEE05;
    }
    .small-font{
	font-size: 0.8em;
    }
    .table a{
	/*color: #080808;*/
    }
    .waiting-icon{
	width: 2em;
	height: 2em;
    }
    .value-equal{
	background: #ccccff;
    }
    .value-greater{
	background: #ff6633;
	color: #fff;
    }
    .value-less{
	background: #cc0000;
	color: #fff;
    }
    <?php foreach ($this->codes as $c): ?>
        .<?php echo $c->code; ?>{background: <?php echo $c->couleur; ?>}
    <?php endforeach; ?>
    <?php if ($nohead): ?>
        .template-edit{
    	cursor: not-allowed;
        }
    <?php endif; ?>
    .gobackBtn{
	position: absolute;
	opacity: 0.5;
	top: 1%;
	right: 3%;
    }
    .gobackBtn:hover{
	opacity: 1.0;
    }
    .evolution{
	display: block;
	width: 400px;
	height: 1.5em;
	background: #66ccff;
	border: solid 1px #6699ff;
	margin-top: 10px;
	margin-left: 210px;
    }
    .evolution .title{
	width: 70%;
	display: inline-block;
    }
    .evolution select{
	display: inline-block;
	width: 29%;
	float: right;
	height: 100%;
	border: none;
    }
    .black-link a{
	color: #000;
    }
    .popover{
	width: 550px;
    }
</style>